public class MyQueue {
    public static class ListNode{
        ListNode next;
        ListNode prev;
        int val;
        ListNode(int val){
            this.val = val;
        }
    }
    ListNode first;//队头
    ListNode last;//队尾
    int usedSize = 0;//队列的大小
    //入队列
    public void offer(int val){
        ListNode node = new ListNode(val);
        if(first == null){//队列中没有元素
            first = node;
        }else{
            last.next = node;
            node.prev = last;
        }
        last = node;//更换队尾
        usedSize++;
    }
    //出队列
    public int poll(){
        int value = -1;
        if(first == null){
            System.out.println("队列为空！");
            return value;
        }else if(first == last){
            value = first.val;
            first = null;
            last = null;
        }else {
            value = first.val;//存储队头元素
            first = first.next;//将队头移向下一个
            first.prev.next = null;
            first.prev = null;
        }
        return value;
    }
    //获得队头元素
    public int peek(){
        if(first == null){
            System.out.println("队列为空！");
            return -1;
        }
        return first.val;
    }
}
